<?php
namespace App\Models;

class ClientUserIntegral extends CommonModel {
	protected $table = 'client_user_integral';
	
	
	/**
	 * @param $uid
	 * @param $type_id
	 * @param null $integral
	 * @return mixed
	 * 添加一条积分记录数据，使用  ClientUserIntegral::create()->data(参数)->save();
	 */
	public function data($uid,$type_id,$integral = null){
		$type = ClientUserIntegralType::select('id','name','integral')->where(['id'=>$type_id])->first();
		$integral_overage = static::where(['uid'=>$uid])->latest()->value('integral_after') ?? 0;
		$integral = $integral ?? $type->integral;
		
		$add = [
			'uid'		=> $uid,
			'type'		=> $type_id,
			'name'		=> $type->name,
			'integral'	=> $integral,
			'integral_after' => $integral_overage + $integral,
			'status'	=> 1,
		];
		$this->fill($add);
		return $this;
	}
}